package org.example.zpqy.service;

import org.example.zpqy.bean.AppleGround;
import org.example.zpqy.repository.AppleGroundRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class AppleGroundService {
    private final AppleGroundRepository repository;
    private final AtomicInteger currentIndex = new AtomicInteger(0); // 用于追踪当前显示的索引

    public AppleGroundService(AppleGroundRepository repository) {
        this.repository = repository;
    }

    // 获取当前索引对应的一行数据，并自动切换到下一行（循环）
    public AppleGround getNextData() {
        List<AppleGround> allData = repository.findAllOrderedById();
        if (allData.isEmpty()) {
            return null;
        }

        int index = currentIndex.getAndUpdate(i -> (i + 1) % allData.size());
        return allData.get(index);
    }
}